1 Explanations

pH and Eh are related, but not entirely dependent on each other. Eh is only related to pH for reactions involving protons or hydroxide, because these affect pH. The more energy-generating reactions are occuring, the more negative we expect the Eh to be (More negative = more reduced). For example the TCA cycle creates NADH and FADH2 which are both reduced by gaining e- with H.

2 pH is expected to increase slightly with exposure to air because CO2 dissolved in the rumen fluid will be lost to air.

As mentioned, when carbon dioxide is dissolved in water, the release of hydrogen ions in carbonic acid is what lowers the pH. As CO2 levels increase around Earth, the amount of dissolved CO2 also increases, which increases the amount of carbonic acid, therefore decreasing the pH

CO2 (aq) + H2O ⇌ H2CO3 (aq) Carbon dioxide + Water ⇌ Carbonic Acid

Henry’s Law of the Solubility of Gases Henry’s Law is a law of physics formulated by William Henry in 1803. It states:

“At a constant temperature, the amount of a given gas that dissolves in a given type and volume of liquid is directly proportional to the partial pressure of that gas in equilibrium with that liquid.” An equivalent way of stating the law is that the solubility of a gas in a liquid is directly proportional to the partial pressure of the gas above the liquid.” (emphasis added)3

https://pubmed.ncbi.nlm.nih.gov/22700881/#&gid=article-figures&pid=figure-1-uid-0 https://blog.orendatech.com/co2-and-ph-henrys-law

3 Load packages

# Load packages-----------------------------------------------------------------
library(readxl)
library(tidyverse)
library(lubridate)
library(lmerTest)
library(lme4)
library(broom.mixed)
library(emmeans)
library(ggplot2)
library(afex)
library(simputation)
library(plotly)
library(MAW636)
afex::set_sum_contrasts()
theme_set(theme_bw())

#—————————————————————
# treatment schedule
trtsched_upload <- read_excel("00_input/MAW636 Treatment Schedule.xlsx") %>%
  pivot_longer(cols = -Cow, names_to = "period", values_to = "trt") %>%
  rename(cow = "Cow") %>%
  mutate(cow = as.character(cow)) %>%
  na_if("NA") %>%
  drop_na() %>%
  separate(trt, c("osc_lvl", "prot_lvl"), sep = cumsum(c(1, 2))) %>%
  mutate(trt = paste0(osc_lvl, prot_lvl)) # separate into factorial trt


#—————————————————————
 
rumen <- read_xlsx("00_input/MAW636 GD Data Upload.xlsx", sheet = "rumen_ph") %>% 
  mutate(day = factor(day, levels = c("Day1", "Day2", "Day3", "Day4"))) %>% 
  mutate(day_n = as.numeric(day)) %>% 
  mutate(time_n = (day_n*24-32)+hr) %>% 
  mutate(cow = as.character(cow)) %>% 
  mutate(rumen_ph = as.numeric(rumen_ph)) %>% 
  left_join(trtsched_upload) %>% 
  filter(rumen_ph >4 & rumen_ph<8)## remove outliers

csv_out(rumen,"org")

4 Plot

logger <- read_excel("00_input/Logger Data/MAW636 combined logger data.xlsx") %>% 
  mutate(pH = as.numeric(pH),
         cow = as.character(cow)) %>% 
  mutate(date2 = as_date(date)) %>% 
  filter(date2!= "2021-07-24") %>% 
  filter(ymd_hms(date) <= ymd_hms("2021-08-06 17:00:00")) %>% 
  mutate(ct = cut(date, breaks = "5 min")) %>% 
  left_join(trtsched_upload %>% filter(period == "P4")) 
## Warning in mask$eval_all_mutate(quo): NAs introduced by coercion
## Joining, by = "cow"
logger_o <- logger %>% 
  filter(osc_lvl == "O")

logger_s <- logger %>% 
  filter(osc_lvl == "S")

logger_sum_o <- logger_o %>% 
  group_by(ct) %>% 
  mutate(date3 = min(date)) %>% 
  group_by(ct, date3, date2, cow) %>% 
  summarise(ph_sum = mean(pH)) %>% 
  ungroup() %>% 
  #mutate(hr = hour(date3)) %>% 
  mutate(osc_day = case_when(
    date2 == "2021-07-24"| date2 == "2021-07-25" ~ "Low",
    date2 == "2021-07-26"| date2 == "2021-07-27" ~ "High",
    date2 == "2021-07-28"| date2 == "2021-07-29" ~ "Low",
    date2 == "2021-07-30"| date2 == "2021-07-31" ~ "High",
    date2 == "2021-08-01"| date2 == "2021-08-02" ~ "Low",
    date2 == "2021-08-03"| date2 == "2021-08-04" ~ "High",
    date2 == "2021-08-05"| date2 == "2021-08-06" ~ "Low")) #%>% 
## `summarise()` has grouped output by 'ct', 'date3', 'date2'. You can override
## using the `.groups` argument.
  #mutate(day = cut(date2))

logger_sum_s <- logger_s %>% 
  group_by(ct) %>% 
  mutate(date3 = min(date)) %>% 
  group_by(ct, date3, date2, cow) %>% 
  summarise(ph_sum = mean(pH)) %>% 
  ungroup() %>% 
  #mutate(hr = hour(date3)) %>% 
  mutate(osc_day = case_when(
    date2 == "2021-07-24"| date2 == "2021-07-25" ~ "Low",
    date2 == "2021-07-26"| date2 == "2021-07-27" ~ "High",
    date2 == "2021-07-28"| date2 == "2021-07-29" ~ "Low",
    date2 == "2021-07-30"| date2 == "2021-07-31" ~ "High",
    date2 == "2021-08-01"| date2 == "2021-08-02" ~ "Low",
    date2 == "2021-08-03"| date2 == "2021-08-04" ~ "High",
    date2 == "2021-08-05"| date2 == "2021-08-06" ~ "Low")) #%>% 
## `summarise()` has grouped output by 'ct', 'date3', 'date2'. You can override
## using the `.groups` argument.
  #mutate(day = cut(date2))
    

  
g2 = logger_sum_o %>% 
  group_by(cow) %>% 
  plot_ly(., x = ~date3, y = ~ph_sum, color = ~cow, type = "scatter", mode = "lines")

g2%>%
  layout(., 
         title='OSCILLATING 5-min. rolling average of rumen pH from indwelling loggers',
         xaxis = list(rangeslider = list(visible = T)),
         shapes = list(
    list(
      type = "rect",
      fillcolor = "white", opacity = 0.1,
      y0 = 5, y1 = 7.35, 
      x0 = as_datetime("2021-07-25 00:05:09"),
      x1 = as_datetime("2021-07-26 08:05:09")
    ),
    list(
      type = "rect",
      fillcolor = "grey",  opacity = 0.1,
      y0 = 5, y1 = 7.35, line_width = 0,
      x0 = as_datetime("2021-07-26 08:05:09"),
      x1 = as_datetime("2021-07-28 08:05:09")
    ),
        list(
      type = "rect",
      fillcolor = "white", opacity = 0.1,
      y0 = 5, y1 = 7.35, line_width = 0,
      x0 = as_datetime("2021-07-28 08:00:09"),
      x1 = as_datetime("2021-07-30 08:05:09")
    ),
        list(
      type = "rect",
      fillcolor = "grey",  opacity = 0.1,
      y0 = 5, y1 = 7.35, line_width = 0,
      x0 = as_datetime("2021-07-30 08:05:09"),
      x1 = as_datetime("2021-08-01 08:05:09")
    ),
        list(
      type = "rect",
      fillcolor = "white", opacity = 0.1,
      y0 = 5, y1 = 7.35, line_width = 0,
      x0 = as_datetime("2021-08-01 08:05:09"),
      x1 = as_datetime("2021-08-03 08:05:09")
    ),
        list(
      type = "rect",
      fillcolor = "grey",  opacity = 0.1,
      y0 = 5, y1 = 7.35, line_width = 0,
      x0 = as_datetime("2021-08-03 08:05:09"),
      x1 = as_datetime("2021-08-05 08:05:09")
  ),
        list(
      type = "rect",
      fillcolor = "white", opacity = 0.1,
      y0 = 5, y1 = 7.35, line_width = 0,
      x0 = as_datetime("2021-08-05 08:05:09"),
      x1 = as_datetime("2021-08-06 17:05:09")),
        list(
        type = "rect",
        fillcolor = "white", opacity = 0.1,
        y0 = 5, y1 = 7.35, line_width = 0,
        x0 = as_datetime("2021-08-05 08:05:09"),
        x1 = as_datetime("2021-08-06 17:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-25 08:05:09"),
        x1 = as_datetime("2021-07-25 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-26 08:05:09"),
        x1 = as_datetime("2021-07-26 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-27 08:05:09"),
        x1 = as_datetime("2021-07-27 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-28 08:05:09"),
        x1 = as_datetime("2021-07-28 08:05:09")
      ),
      list(
        type = "line", dash = "dash",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-29 08:05:09"),
        x1 = as_datetime("2021-07-29 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-30 08:05:09"),
        x1 = as_datetime("2021-07-30 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-31 08:05:09"),
        x1 = as_datetime("2021-07-31 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-01 08:05:09"),
        x1 = as_datetime("2021-08-01 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-02 08:05:09"),
        x1 = as_datetime("2021-08-02 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-03 08:05:09"),
        x1 = as_datetime("2021-08-03 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-04 08:05:09"),
        x1 = as_datetime("2021-08-04 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-05 08:05:09"),
        x1 = as_datetime("2021-08-05 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-06 08:05:09"),
        x1 = as_datetime("2021-08-06 08:05:09")
      )))
g3 = logger_sum_s %>% 
  group_by(cow) %>% 
  plot_ly(., x = ~date3, y = ~ph_sum, color = ~cow, type = "scatter", mode = "lines")


g3 %>%
  layout(.,
    title = "STATIC 5-min. rolling average of rumen pH from indwelling loggers",
    xaxis = list(rangeslider = list(visible = T)),
    shapes = list(
      list(
        arrow = 1,
        type = "rect",
        fillcolor = "white", opacity = 0.1,
        y0 = 5, y1 = 7.35,
        x0 = as_datetime("2021-07-25 00:05:09"),
        x1 = as_datetime("2021-07-26 08:05:09")
      ),
      list(
        type = "rect",
        fillcolor = "grey", opacity = 0.1,
        y0 = 5, y1 = 7.35, line_width = 0,
        x0 = as_datetime("2021-07-26 08:05:09"),
        x1 = as_datetime("2021-07-28 08:05:09")
      ),
      list(
        type = "rect",
        fillcolor = "white", opacity = 0.1,
        y0 = 5, y1 = 7.35, line_width = 0,
        x0 = as_datetime("2021-07-28 08:00:09"),
        x1 = as_datetime("2021-07-30 08:05:09")
      ),
      list(
        type = "rect",
        fillcolor = "grey",  opacity = 0.1,
        y0 = 5, y1 = 7.35, line_width = 0,
        x0 = as_datetime("2021-07-30 08:05:09"),
        x1 = as_datetime("2021-08-01 08:05:09")
      ),
      list(
        type = "rect",
        fillcolor = "white", opacity = 0.1,
        y0 = 5, y1 = 7.35, line_width = 0,
        x0 = as_datetime("2021-08-01 08:05:09"),
        x1 = as_datetime("2021-08-03 08:05:09")
      ),
      list(
        type = "rect",
        fillcolor = "grey",  opacity = 0.1,
        y0 = 5, y1 = 7.35, line_width = 0,
        x0 = as_datetime("2021-08-03 08:05:09"),
        x1 = as_datetime("2021-08-05 08:05:09")
      ),
      list(
        type = "rect",
        fillcolor = "white", opacity = 0.1,
        y0 = 5, y1 = 7.35, line_width = 0,
        x0 = as_datetime("2021-08-05 08:05:09"),
        x1 = as_datetime("2021-08-06 17:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-25 08:05:09"),
        x1 = as_datetime("2021-07-25 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-26 08:05:09"),
        x1 = as_datetime("2021-07-26 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-27 08:05:09"),
        x1 = as_datetime("2021-07-27 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-28 08:05:09"),
        x1 = as_datetime("2021-07-28 08:05:09")
      ),
      list(
        type = "line", 
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-29 08:05:09"),
        x1 = as_datetime("2021-07-29 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-30 08:05:09"),
        x1 = as_datetime("2021-07-30 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-07-31 08:05:09"),
        x1 = as_datetime("2021-07-31 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-01 08:05:09"),
        x1 = as_datetime("2021-08-01 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-02 08:05:09"),
        x1 = as_datetime("2021-08-02 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-03 08:05:09"),
        x1 = as_datetime("2021-08-03 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-04 08:05:09"),
        x1 = as_datetime("2021-08-04 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-05 08:05:09"),
        x1 = as_datetime("2021-08-05 08:05:09")
      ),
      list(
        type = "line",
        fillcolor = "blue", opacity = 1,
        y0 = 7, y1 = 7.35,
        x0 = as_datetime("2021-08-06 08:05:09"),
        x1 = as_datetime("2021-08-06 08:05:09")
      )
    )
  )

4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1

To be completed

##################################

5 Temp

logger_sum_o <- logger_o %>% 
  group_by(ct) %>% 
  mutate(date3 = min(date)) %>% 
  group_by(ct, date3, date2, cow) %>% 
  summarise(temp_sum = mean(temp)) %>% 
  ungroup() %>% 
  #mutate(hr = hour(date3)) %>% 
  mutate(osc_day = case_when(
    date2 == "2021-07-24"| date2 == "2021-07-25" ~ "Low",
    date2 == "2021-07-26"| date2 == "2021-07-27" ~ "High",
    date2 == "2021-07-28"| date2 == "2021-07-29" ~ "Low",
    date2 == "2021-07-30"| date2 == "2021-07-31" ~ "High",
    date2 == "2021-08-01"| date2 == "2021-08-02" ~ "Low",
    date2 == "2021-08-03"| date2 == "2021-08-04" ~ "High",
    date2 == "2021-08-05"| date2 == "2021-08-06" ~ "Low")) #%>% 
  #mutate(day = cut(date2))

logger_sum_s <- logger_s %>% 
  group_by(ct) %>% 
  mutate(date3 = min(date)) %>% 
  group_by(ct, date3, date2, cow) %>% 
  summarise(temp_sum = mean(temp)) %>% 
  ungroup() %>% 
  #mutate(hr = hour(date3)) %>% 
  mutate(osc_day = case_when(
    date2 == "2021-07-24"| date2 == "2021-07-25" ~ "Low",
    date2 == "2021-07-26"| date2 == "2021-07-27" ~ "High",
    date2 == "2021-07-28"| date2 == "2021-07-29" ~ "Low",
    date2 == "2021-07-30"| date2 == "2021-07-31" ~ "High",
    date2 == "2021-08-01"| date2 == "2021-08-02" ~ "Low",
    date2 == "2021-08-03"| date2 == "2021-08-04" ~ "High",
    date2 == "2021-08-05"| date2 == "2021-08-06" ~ "Low")) #%>% 
  #mutate(day = cut(date2))
   

g3 = logger_sum_s %>% 
  group_by(cow) %>% 
  plot_ly(., x = ~date3, y = ~temp_sum, color = ~cow, type = "scatter", mode = "lines")


g3 %>%
  layout(.,
    title = "STATIC 5-min. rolling average of rumen temperature from indwelling loggers",
    xaxis = list(rangeslider = list(visible = T))
  )

6 Redox potential

logger_sum_o <- logger_o %>% 
  group_by(ct) %>% 
  mutate(date3 = min(date)) %>% 
  group_by(ct, date3, date2, cow) %>% 
  summarise(mv_sum = mean(as.numeric(mv))) %>% 
  ungroup() %>% 
  #mutate(hr = hour(date3)) %>% 
  mutate(osc_day = case_when(
    date2 == "2021-07-24"| date2 == "2021-07-25" ~ "Low",
    date2 == "2021-07-26"| date2 == "2021-07-27" ~ "High",
    date2 == "2021-07-28"| date2 == "2021-07-29" ~ "Low",
    date2 == "2021-07-30"| date2 == "2021-07-31" ~ "High",
    date2 == "2021-08-01"| date2 == "2021-08-02" ~ "Low",
    date2 == "2021-08-03"| date2 == "2021-08-04" ~ "High",
    date2 == "2021-08-05"| date2 == "2021-08-06" ~ "Low")) #%>% 
  #mutate(day = cut(date2))

logger_sum_s <- logger_s %>% 
  group_by(ct) %>% 
  mutate(date3 = min(date)) %>% 
  group_by(ct, date3, date2, cow) %>% 
  summarise(mv_sum =  mean(as.numeric(mv))) %>% 
  ungroup() %>% 
  #mutate(hr = hour(date3)) %>% 
  mutate(osc_day = case_when(
    date2 == "2021-07-24"| date2 == "2021-07-25" ~ "Low",
    date2 == "2021-07-26"| date2 == "2021-07-27" ~ "High",
    date2 == "2021-07-28"| date2 == "2021-07-29" ~ "Low",
    date2 == "2021-07-30"| date2 == "2021-07-31" ~ "High",
    date2 == "2021-08-01"| date2 == "2021-08-02" ~ "Low",
    date2 == "2021-08-03"| date2 == "2021-08-04" ~ "High",
    date2 == "2021-08-05"| date2 == "2021-08-06" ~ "Low")) #%>% 
  #mutate(day = cut(date2))
   

g3 = logger_sum_s %>% 
  group_by(cow) %>% 
  plot_ly(., x = ~date3, y = ~mv_sum, color = ~cow, type = "scatter", mode = "lines")


g3 %>%
  layout(.,
    title = "STATIC 5-min. rolling average of rumen redox potential from indwelling loggers",
    xaxis = list(rangeslider = list(visible = T))
  )

7/24/21 7/25/21 Low 7/26/21 7/27/21 High 7/28/21 7/29/21 Low 7/30/21 7/31/21 High 8/1/21 8/2/21 Low 8/3/21 8/4/21 High 8/5/21 8/6/21 Low 8/7/21 8/8/21 High

g2 %>%
  layout(., shapes = lines)
  layout(showlegend = F, title='Time Series with Rangeslider',
         xaxis = list(rangeslider = list(visible = T)))

p1 <- logger %>% 
  ggplot(aes(x = date, y = pH, group = cow, col = cow)) + geom_point(size = .2)

ggplotly(p1)

p2 <- logger_sum_o %>% 
  ggplot(aes(x = as_datetime(date3), y = ph_sum, group = cow, col = cow)) +
  geom_point(size = .2) + geom_line() + scale_x_datetime(date_breaks = "1 day") +
  theme(axis.text.x = element_text(angle = 90)) +
  geom_rect(aes(xmin = date3, xmax = (date3)+300, 
                ymin = -Inf, ymax = Inf, fill = osc_day), color = "NA", alpha = .02)
p2

as_datetime(logger_sum$date3)[1]
as_datetime(logger_sum$date3)[10000]

g2 = ggplotly(p2)

case_when(hr = 8)
seq(1, 10, 1)
?seq

line <- list(
  type = "line",
  line = list(color = "pink"),
  xref = "date3",
  yref = "ph_sum"
)

lines <- list()
for (i in seq(1,10, 1)) {
  line[["x0"]] <- as_datetime("2021-07-25 00:05:09")
  line[["x1"]] <- as_datetime("2021-07-25 00:05:09") + i
  line[["y0"]] <- 5
  line[["y1"]] <- 7.35
  lines<- c(lines, list(line))
}

list(type = "line", linetype = "dashed",
                y0 = 5, y1 = 7.35, 
                x0 = as_datetime("2021-07-25 00:05:09"), 
                x1 = as_datetime("2021-07-25 00:05:09")),

           list(type = "line", dash = "dash",
                y0 = 5, y1 = 7.35, 
                x0 = as_datetime("2021-07-25 00:05:09"), 
                x1 = as_datetime("2021-07-25 00:05:09")),